home *** CD-ROM | disk | FTP | other *** search
- /*
- File: MakeStartupAliasToMe.c
-
- Description:
- This small application illustrating how to launch an AppleScript
- and send a list of files to it for processing. This a simple way
- for your application to access the powerful high level facilities
- provided by AppleScript.
-
- Copyright:
- © Copyright 2000 Apple Computer, Inc. All rights reserved.
-
- Disclaimer:
- IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
- ("Apple") in consideration of your agreement to the following terms, and your
- use, installation, modification or redistribution of this Apple software
- constitutes acceptance of these terms. If you do not agree with these terms,
- please do not use, install, modify or redistribute this Apple software.
-
- In consideration of your agreement to abide by the following terms, and subject
- to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
- copyrights in this original Apple software (the "Apple Software"), to use,
- reproduce, modify and redistribute the Apple Software, with or without
- modifications, in source and/or binary forms; provided that if you redistribute
- the Apple Software in its entirety and without modifications, you must retain
- this notice and the following text and disclaimers in all such redistributions of
- the Apple Software. Neither the name, trademarks, service marks or logos of
- Apple Computer, Inc. may be used to endorse or promote products derived from the
- Apple Software without specific prior written permission from Apple. Except as
- expressly stated in this notice, no other rights or licenses, express or implied,
- are granted by Apple herein, including but not limited to any patent rights that
- may be infringed by your derivative works or by other works in which the Apple
- Software may be incorporated.
-
- The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
- WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
- WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
- COMBINATION WITH YOUR PRODUCTS.
-
- IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
- OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
- (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- Change History (most recent first):
- Monday, March 27, 2000 -- created
- */
-
-
- #include <Types.h>
- #include <QuickDraw.h>
- #include <Processes.h>
- #include <Aliases.h>
- #include <Sound.h>
- #include <PLStringFuncs.h>
- #include <Resources.h>
- #include <Files.h>
- #include <Folders.h>
- #include <TextUtils.h>
-
- #include "debugf.h"
-
-
- /* kScriptNameSTRID is the resource ID number for a resource
- of type 'STR ' stored in the application's resource file. This resource
- contains the name of the AppleScript we will launch to create the alias */
- #define kScriptNameSTRID 128
-
- /* kScriptLocationsStringListID contains the resource ID of a string
- list resource (type 'STR#') stored in the application's resource fork.
- The strings stored in this resource list the names of folders inside
- of the same folder where the application resides where the program
- will search for the script named in the 'STR ' resource described above. */
- #define kScriptLocationsStringListID 128
-
- /* kLaunchFailedAlertID is the resource ID of the alert displayed when
- a problem occurs either finding or launching the script. It does not
- display errors reported by the script itself--it is up to the script
- to report those errors. */
- #define kLaunchFailedAlertID 128
-
-
-
- /* FindCurrentApplication locates the current application's
- file and returns a reference to it in the file specification
- record provided as a parameter. */
- static OSStatus FindCurrentApplication(FSSpec *spec) {
- OSStatus err;
- ProcessSerialNumber PSN;
- ProcessInfoRec info;
- /* get the current process ID */
- err = GetCurrentProcess(&PSN);
- if (err != noErr) return err;
- /* get the current process location */
- BlockZero(&info, sizeof(info));
- info.processInfoLength = sizeof(ProcessInfoRec);
- info.processAppSpec = spec;
- err = GetProcessInformation(&PSN, &info);
- if (err != noErr) return err;
- /* done */
- return noErr;
- }
-
-
- /* LaunchScriptWithFiles launches the script referenced by the *asDroplet
- file specification record and sends the list of files referenced by fileList to
- it for processing. For this routine to work, the script must:
- (a) be saved as an applet,
- (b) the applet's script must contain a "on open ..." block for
- processing the list of files sent to it by this routine.
- (c) display any error alerts should incident occur.
- LaunchScriptWithFiles will report any errors that occur while constructing
- the list of files to send to the script and while launching the script, but
- it will not report any errors that may occur while the script is being
- processed. As a result, if the script encounters any errors, it should
- display an appropriate alert describing the problem that occured. */
- static OSStatus LaunchScriptWithFiles(FSSpec *asDroplet, FSSpec *fileList, long fileCount) {
- OSErr err;
- AEAddressDesc appParamsAETargetAddr;
- AEDescList targetFileListDesc;
- AEDesc appParamsDesc;
- AppleEvent appParamsAE;
- AliasHandle targetFileAlias;
- FInfo fndrInfo;
- AppParametersPtr appParams;
- LaunchParamBlockRec launchParam;
- long bytecount, index;
- FSSpec *nthFile;
-
- /* initialize our records to NULL descriptors */
- AECreateDesc(typeNull, NULL, 0, &appParamsAETargetAddr);
- AECreateDesc(typeNull, NULL, 0, &targetFileListDesc);
- AECreateDesc(typeNull, NULL, 0, &appParamsAE);
- AECreateDesc(typeNull, NULL, 0, &appParamsDesc);
- targetFileAlias = NULL;
- appParams = NULL;
-
- /* create an open documents Apple event */
- err = FSpGetFInfo(asDroplet, &fndrInfo);
- if (err != noErr) goto bail;
- err = AECreateDesc(typeApplSignature, (Ptr)&fndrInfo.fdCreator,
- sizeof(OSType), &appParamsAETargetAddr);
- if (err != noErr) goto bail;
- err = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments,
- &appParamsAETargetAddr, kAutoGenerateReturnID, kAnyTransactionID, &appParamsAE);
- if (err != noErr) goto bail;
-
- /* create a list of files to send to the droplet */
- err = AECreateList(NULL, 0, false, &targetFileListDesc);
- if (err != noErr) goto bail;
- for (nthFile = fileList, index=0; index < fileCount; index++, nthFile++) {
- err = NewAlias(NULL, nthFile, &targetFileAlias);
- if (err != noErr) goto bail;
- HLock((Handle) targetFileAlias);
- err = AEPutPtr(&targetFileListDesc, index + 1, typeAlias,
- (Ptr) (*targetFileAlias), GetHandleSize((Handle) targetFileAlias));
- DisposeHandle((Handle) targetFileAlias);
- targetFileAlias = NULL;
- if (err != noErr) goto bail;
- }
-
- /* add the file list to the open documents apple event */
- err = AEPutParamDesc(&appParamsAE, keyDirectObject, &targetFileListDesc);
- if (err != noErr) goto bail;
-
- /* coerce the apple event to app parameters */
- err = AECoerceDesc(&appParamsAE, typeAppParameters, &appParamsDesc);
- if (err != noErr) goto bail;
- bytecount = AEGetDescDataSize(&appParamsDesc);
- appParams = (AppParametersPtr) NewPtr(bytecount);
- if (appParams == NULL) { err = memFullErr; goto bail; }
- err = AEGetDescData(&appParamsDesc, appParams, bytecount);
- if (err != noErr) goto bail;
-
- /* launch the application */
- BlockZero(&launchParam, sizeof(launchParam));
- launchParam.launchBlockID = extendedBlock;
- launchParam.launchEPBLength = extendedBlockLen;
- launchParam.launchFileFlags = 0;
- launchParam.launchControlFlags = launchContinue + launchNoFileFlags;
- launchParam.launchAppSpec = asDroplet;
- launchParam.launchAppParameters = appParams;
- err = LaunchApplication(&launchParam);
-
- bail:
- if (appParams != NULL) DisposePtr((Ptr) appParams);
- if (targetFileAlias != NULL) DisposeHandle((Handle) targetFileAlias);
- AEDisposeDesc(&appParamsAE);
- AEDisposeDesc(&appParamsAETargetAddr);
- AEDisposeDesc(&appParamsDesc);
- AEDisposeDesc(&targetFileListDesc);
- return err;
- }
-
-
- /* LocateExecutableScriptFile locates the AppleScript named scriptName.
- If appRelPathsID is not zero, then LocateExecutableScriptFile will search
- the folders named in the string list resource with that ID inside of the
- application's directory before searching the system's script folder. If
- a script with a matching name is found, then a reference will be returned
- in the *targetScript file specification record. */
- static OSStatus LocateExecutableScriptFile(StringPtr scriptName, short appRelPathsID, FSSpec *targetScript) {
- OSStatus err;
- ProcessSerialNumber PSN;
- ProcessInfoRec info;
- short **paths, i, n, vRefNum;
- long dirID;
- FSSpec appSpec, theScript;
- Str255 theName;
- CInfoPBRec cat;
- /* locate the application's folder using our custom paths*/
- if (appRelPathsID != 0) {
- paths = (short **) GetResource('STR#', appRelPathsID);
- if (paths == NULL) { err = resNotFound; return err; }
- n = **paths;
- /* find the application's location */
- if ((err = GetCurrentProcess(&PSN)) != noErr) return err;
- BlockZero(&info, sizeof(info));
- info.processInfoLength = sizeof(ProcessInfoRec);
- info.processAppSpec = &appSpec;
- if ((err = GetProcessInformation(&PSN, &info)) != noErr) return err;
- /* search application relative paths */
- for (i=1; i<= n; i++) {
- GetIndString(theName, appRelPathsID, i);
- BlockZero(&cat, sizeof(cat));
- cat.hFileInfo.ioNamePtr = theName;
- cat.hFileInfo.ioVRefNum = appSpec.vRefNum;
- cat.hFileInfo.ioDirID = appSpec.parID;
- if (PBGetCatInfoSync(&cat) == noErr)
- if ((cat.hFileInfo.ioFlAttrib & 16) != 0)
- if (FSMakeFSSpec(appSpec.vRefNum, cat.hFileInfo.ioDirID, scriptName, &theScript) == noErr) {
- *targetScript = theScript;
- return noErr;
- }
- }
- }
- /* not there, use the scripts folder */
- if (FindFolder(kOnSystemDisk, kScriptsFolderType, false, &vRefNum, &dirID) == noErr) {
- if (FSMakeFSSpec(vRefNum, dirID, scriptName, &theScript) == noErr) {
- *targetScript = theScript;
- return noErr;
- }
- }
- /* we failed */
- return fnfErr;
- }
-
-
-
-
-
- int main(void) {
- FSSpec theApp;
- FSSpec theScript;
- OSStatus err;
- Str255 errStr;
- Str255 scriptName;
- StringHandle scriptNameHandle;
-
- /* get the name of the script */
- scriptNameHandle = GetString(kScriptNameSTRID);
- if (scriptNameHandle == NULL) {
- PLstrcpy(scriptName, "\punknown script");
- err = resNotFound;
- goto bail;
- } else {
- HLock((Handle) scriptNameHandle);
- PLstrcpy(scriptName, *scriptNameHandle);
- HUnlock((Handle) scriptNameHandle);
- }
-
- /* locate the script file */
- err = LocateExecutableScriptFile(scriptName, kScriptLocationsStringListID, &theScript);
- if (err != noErr) goto bail;
-
- /* create a reference to our application */
- err = FindCurrentApplication(&theApp);
- if (err != noErr) goto bail;
-
- /* launch the script providing our application reference as a parameter */
- err = LaunchScriptWithFiles(&theScript, &theApp, 1);
- if (err != noErr) goto bail;
-
- return 0;
- bail:
- InitCursor();
- NumToString(err, errStr);
- ParamText(scriptName, errStr, NULL, NULL);
- StopAlert(kLaunchFailedAlertID, NULL);
- return 1;
- }